using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Controls.Primitives;
namespace System.Windows.Interactivity
{
	[DefaultTrigger(typeof(UIElement), typeof(EventTrigger), "MouseLeftButtonDown"), DefaultTrigger(typeof(ButtonBase), typeof(EventTrigger), "Click")]
	public abstract class TriggerAction : DependencyObject, IAttachedObject
	{
		private bool isHosted;
		private DependencyObject associatedObject;
		private Type associatedObjectTypeConstraint;
		public static readonly DependencyProperty IsEnabledProperty = DependencyProperty.Register("IsEnabled", typeof(bool), typeof(TriggerAction), new PropertyMetadata(true));
		[DefaultValue(true)]
		public bool IsEnabled
		{
			get
			{
				return (bool)base.GetValue(TriggerAction.IsEnabledProperty);
			}
			set
			{
				base.SetValue(TriggerAction.IsEnabledProperty, value);
			}
		}
		protected DependencyObject AssociatedObject
		{
			get
			{
				return this.associatedObject;
			}
		}
		protected virtual Type AssociatedObjectTypeConstraint
		{
			get
			{
				return this.associatedObjectTypeConstraint;
			}
		}
		internal bool IsHosted
		{
			get
			{
				return this.isHosted;
			}
			set
			{
				this.isHosted = value;
			}
		}
		DependencyObject IAttachedObject.AssociatedObject
		{
			get
			{
				return this.AssociatedObject;
			}
		}
		internal TriggerAction(Type associatedObjectTypeConstraint)
		{
			this.associatedObjectTypeConstraint = associatedObjectTypeConstraint;
		}
		internal void CallInvoke(object parameter)
		{
			if (this.IsEnabled)
			{
				this.Invoke(parameter);
			}
		}
		protected abstract void Invoke(object parameter);
		protected virtual void OnAttached()
		{
		}
		protected virtual void OnDetaching()
		{
		}
		public void Attach(DependencyObject dependencyObject)
		{
			if (dependencyObject != this.AssociatedObject)
			{
				if (this.AssociatedObject != null)
				{
					throw new InvalidOperationException(ExceptionStringTable.CannotHostTriggerActionMultipleTimesExceptionMessage);
				}
				if (dependencyObject != null && !this.AssociatedObjectTypeConstraint.IsAssignableFrom(dependencyObject.GetType()))
				{
					throw new InvalidOperationException(string.Format(CultureInfo.get_CurrentCulture(), ExceptionStringTable.TypeConstraintViolatedExceptionMessage, new object[]
					{
						base.GetType().get_Name(),
						dependencyObject.GetType().get_Name(),
						this.AssociatedObjectTypeConstraint.get_Name()
					}));
				}
				this.associatedObject = dependencyObject;
				this.OnAttached();
			}
		}
		public void Detach()
		{
			this.OnDetaching();
			this.associatedObject = null;
		}
	}
	public abstract class TriggerAction<T> : TriggerAction where T : DependencyObject
	{
		protected new T AssociatedObject
		{
			get
			{
				return (T)((object)base.AssociatedObject);
			}
		}
		protected sealed override Type AssociatedObjectTypeConstraint
		{
			get
			{
				return base.AssociatedObjectTypeConstraint;
			}
		}
		protected TriggerAction() : base(typeof(T))
		{
		}
	}
}
